package me.firstapp.service.bbs.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.utils.Page;
import me.firstapp.module.bbs.BbsNotification;
import me.firstapp.module.bbs.Collection;
import me.firstapp.module.bbs.Member;
import me.firstapp.module.bbs.Topic;
import me.firstapp.module.shop.Product;
import me.firstapp.repository.bbs.BbsNotificationRepository;
import me.firstapp.repository.bbs.CollectionRepository;
import me.firstapp.repository.bbs.MemberRepository;
import me.firstapp.repository.bbs.TopicRepository;
import me.firstapp.repository.shop.ProductRepository;
import me.firstapp.service.bbs.CollectionService;

@Service
public class CollectionServiceImpl implements CollectionService {

	@Autowired
	private CollectionRepository collectionRepository;

	@Autowired
	private MemberRepository memberRepository;

	@Autowired
	private TopicRepository topicRepository;

	@Autowired
	private ProductRepository productRepository;

	@Autowired
	private BbsNotificationRepository bbsNotificationRepository;

	@Override
	public Long findCollectionCounts(Long memberId, Long topicId, Long productId, Long apiClientId) {
		return collectionRepository.findCollectionCounts(memberId, topicId, productId, apiClientId);
	}

	@Override
	public Collection findMemberTopicCollectionRel(Long memberId, Long topicId, Long apiClientId) {
		if (memberId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "会员id");
		}
		if (topicId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "话题id");
		}
		return collectionRepository.findMemberTopicCollectionRel(memberId, topicId, apiClientId);
	}

	@Override
	public Collection findMemberProductCollectionRel(Long memberId, Long productId, Long apiClientId) {
		if (memberId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "会员id");
		}
		if (productId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "商品id");
		}
		return collectionRepository.findMemberProductCollectionRel(memberId, productId, apiClientId);
	}

	@Override
	public Collection addCollection(Long memberId, Long topicId, Long productId, Integer type, Long apiClientId) {
		if (memberId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "用户id");
		}
		Member member = (Member) memberRepository.findMemberById(memberId, apiClientId);
		if (member == null) {
			throw new ServiceException(StatusHouse.MEMBER_NOT_FOUND);
		}
		if (type == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "收藏类型");
		}
		if (type == 0) {
			if (topicId == null) {
				throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "话题id");
			}
			Topic topic = (Topic) topicRepository.findTopicById(topicId, apiClientId);
			if (topic == null) {
				throw new ServiceException(StatusHouse.TOPIC_NOT_EXIST);
			}
			Collection collection = collectionRepository.findMemberTopicCollectionRel(memberId, topicId, apiClientId);
			if (collection != null) {
				throw new ServiceException(StatusHouse.COLLECTION_IS_FOUND);
			}
			if (memberId == topic.getMember().getId()) {
				throw new ServiceException(StatusHouse.CAN_NOT_SELF_COLLECTION);
			}
			collection = new Collection();
			collection.setMember(member);
			collection.setTopic(topic);
			collection.setType(0);
			collection.setApiClientId(apiClientId);
			collectionRepository.save(collection);
			// 通知
			try {
				if (memberId != topic.getMember().getId()) {
					BbsNotification bbsNotification = new BbsNotification();
					bbsNotification.setAction(0);
					bbsNotification.setContent("已收藏");
					bbsNotification.setFromMember(member);
					bbsNotification.setIsRead(0);
					bbsNotification.setNotifyTime(new Date());
					bbsNotification.setToMember(topic.getMember());
					bbsNotification.setTopic(topic);
					bbsNotification.setApiClientId(apiClientId);
					bbsNotificationRepository.save(bbsNotification);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return collection;

		} else if (type == 1) {
			if (productId == null) {
				throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "商品id");
			}
			Product product = (Product) productRepository.findProductById(productId, apiClientId);
			if (product == null) {
				throw new ServiceException(StatusHouse.PRODUCT_NOT_EXIST);
			}
			Collection collection = collectionRepository.findMemberProductCollectionRel(memberId, productId,
					apiClientId);
			if (collection != null) {
				throw new ServiceException(StatusHouse.COLLECTION_IS_FOUND);
			}
			collection = new Collection();
			collection.setMember(member);
			collection.setProduct(product);
			collection.setType(1);
			collection.setApiClientId(apiClientId);
			collectionRepository.save(collection);
			return collection;

		}

		return null;
	}

	@Override
	public void executeCancelCollection(Long memberId, Long topicId, Long productId, Integer type, Long apiClientId) {
		if (memberId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "用户id");
		}
		if (type == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "收藏类型");
		}
		if (type == 0) {
			if (topicId == null) {
				throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "话题id");
			}
			Collection collection = collectionRepository.findMemberTopicCollectionRel(memberId, topicId, apiClientId);
			if (collection == null) {
				throw new ServiceException(StatusHouse.COLLECTION_NOT_FOUND);
			}
			collectionRepository.deleteById(collection.getId());
		} else if (type == 1) {
			if (productId == null) {
				throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "商品id");
			}
			Collection collection = collectionRepository.findMemberProductCollectionRel(memberId, productId,
					apiClientId);
			if (collection == null) {
				throw new ServiceException(StatusHouse.COLLECTION_NOT_FOUND);
			}
			collectionRepository.deleteById(collection.getId());
		}
	}

	@Override
	public Page<Collection> findMemberCollectionsPage(Integer pageNo, Integer pageSize, Long memberId, Integer type,
			Long apiClientId) {
		if (memberId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "会员id");
		}
		if (type == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "收藏类型");
		}
		return collectionRepository.findMemberCollectionsPage(pageNo, pageSize, memberId, type, apiClientId);
	}

}
